package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Permission;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IPermissionService;
import com.woniu.supermarket.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("Permission")
@Api(value = "权限操作")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;

    @GetMapping("/showPermission")
    @ApiOperation(value = "权限分页查询")
    public ResultResponse<PageInfo<Permission>> showPermission(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                               @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size,
                                                               String name){
        ResultResponse<PageInfo<Permission>> result = null;
        try {
            PageInfo<Permission> pageInfo = permissionService.findPermissionByCons(now, size, name);
            result = new ResultResponse<>(200,"查询权限成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询权限失败");
        }
        return result;
    }


    @GetMapping("/listSomeonePermissions/{username}")
    @ApiOperation(value = "查询某个用户权限列表")
    public ResultResponse<List<String>> listSomeoneMenu(@ApiParam(value = "用户名称") @PathVariable String username){
        ResultResponse<List<String>> result = null;
        try {
            List<String> list  = permissionService.queryPermissionsOfOne(username);
            if(list!=null){
                result =  new ResultResponse<>(200,"查询用户权限成功",list);
            }else{
                result =  new ResultResponse<>(201,"查询用户权限失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result =  new ResultResponse<>(555,"系统错误");
        }
        return result;
    }

    @GetMapping("/listSuperAdminPermissions")
    @ApiOperation(value = "查询超级管理员权限列表")
    public ResultResponse<List<String>> listSuperAdminPermissions(){
        ResultResponse<List<String>> result = null;
        try {
            List<String> list  = permissionService.queryPermissionsForSuperAdmin();
            if(list!=null){
                result =  new ResultResponse<>(200,"查询超级管理员权限成功",list);
            }else{
                result =  new ResultResponse<>(201,"查询超级管理员权限失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result =  new ResultResponse<>(555,"系统错误");
        }
        return result;
    }

    @PostMapping("/addPermission")
    @ApiOperation(value = "权限添加操作")
    public ResultResponse<Void> addPermission(@RequestBody Permission permission){
        ResultResponse<Void> result = null;
        try {
            permissionService.addPermission(permission);
            result = new ResultResponse<>(200,"添加权限成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加权限失败");
        }
        return result;
    }


    @PutMapping("/modifyPermission")
    @ApiOperation(value = "修改权限操作")
    public ResultResponse<Void> modifyPermission(@RequestBody Permission permission){
        ResultResponse<Void> result = null;
        try {
            permissionService.modifyPermission(permission);
            result = new ResultResponse<>(200,"修改权限操作");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改权限失败");
        }
        return result;
    }

    @DeleteMapping("/delPermission/{id}")
    @ApiOperation(value = "删除权限操作")
    public ResultResponse<Void> delPermission(@PathVariable("id")Integer id){
        ResultResponse<Void> result = null;
        try {
            permissionService.removePermission(id);
            result = new ResultResponse<>(200,"删除权限成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加权限失败");
        }
        return result;
    }

}
